//
//  CYGHotsBlurView.m
//  CYGHuntForCity
//
//  Created by cygarm on 16/3/11.
//  Copyright © 2016年 cygarm. All rights reserved.
//

#import "CYGHotsBlurView.h"
#import "CYGHotsBlurCell.h"
#import "CYGHotsBlurCellModel.h"
#import <MJExtension.h>

@interface CYGHotsBlurView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UIVisualEffectView *effectView;
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UIButton *cancelBtn;

@property (strong, nonatomic) NSMutableArray<CYGHotsBlurCellModel *> *datas;

@end

@implementation CYGHotsBlurView

- (instancetype)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		[self setupUI];
	}
	
	return self;
}

// 这里可以不用设置image,效果也是一样
+ (instancetype)blurViewWithImage:(UIImage *)image {
	CYGHotsBlurView *view = [[self alloc] init];
	view.image = image;
	return view;
}

- (void)setupUI {
	self.userInteractionEnabled = YES;
	[self addSubview:self.effectView];
	
	self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.cancelBtn setImage:[UIImage imageNamed:@"searchList_btn_delete"] forState:UIControlStateNormal];
	[self.cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
	self.cancelBtn.frame = CGRectZero;
	[self addSubview:self.cancelBtn];
	
	UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
	CGFloat itemWH = 100;
	layout.itemSize = CGSizeMake(itemWH, itemWH);
	layout.minimumInteritemSpacing = 0;
	layout.minimumLineSpacing = 20;
	self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
	self.collectionView.backgroundColor = [UIColor clearColor];
	self.collectionView.delegate = self;
	self.collectionView.dataSource = self;
	[self addSubview:self.collectionView];
	
	[self.collectionView registerNib:[UINib nibWithNibName:@"CYGHotsBlurCell" bundle:nil] forCellWithReuseIdentifier:@"idHotsCell"];
}

- (void)layoutSubviews {
	self.cancelBtn.frame = CGRectMake((CYGAppWidth-30)/2, CYGAppHeight-64-30, 30, 30);
	self.collectionView.frame = CGRectMake(0, self.bounds.origin.y+60, CYGAppWidth, self.bounds.size.height-60-50);
}

- (UIVisualEffectView *)effectView {
	if (_effectView == nil) {
		UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
		_effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
		_effectView.frame = [[UIScreen mainScreen] bounds];
	}
	
	return _effectView;	
}

#pragma mark - IBAction
- (void)cancelBtnClick:(UIButton *)btn {
	[self.delegate CYGHotsBlurViewCancleBtnClick:btn];
}

#pragma mark - UICollectionViewDelegate UICollectionViewDataSource

- (NSMutableArray<CYGHotsBlurCellModel *> *)datas {
	if (_datas == nil) {
		NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"blurData" ofType:@"plist"]];
		_datas = [CYGHotsBlurCellModel mj_objectArrayWithKeyValuesArray:array];
	}
	
	return _datas;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return self.datas.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	CYGHotsBlurCellModel *model = self.datas[indexPath.row];
	CYGHotsBlurCell *cell = [CYGHotsBlurCell cellWithCollectionView:collectionView forIndexPath:indexPath ofModel:model];
	
	return cell;
}

@end
